package com.alibaba.basic.service.impl;

import com.alibaba.basic.mapper.BaseMapper;
import com.alibaba.basic.query.BaseQuery;
import com.alibaba.basic.service.BaseService;
import com.alibaba.basic.uitl.PageList;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;


public class BaseServiceImpl<T> implements BaseService<T> {

    @Autowired
    private BaseMapper<T> mapper;

    @Override
    public List findAll() {
        return mapper.findAll();
    }

    @Override
    public T finById(Long id) {
        return mapper.finById(id);
    }

    @Override
    public void add(T t) {
    mapper.add(t);
    }

    @Override
    public void update(T t) {
    mapper.update(t);
    }

    @Override
    public void delete(Long id) {
    mapper.delete(id);
    }

    @Override
    public void deleteId(List<Long> ids) {
        mapper.deleteId(ids);
    }

    @Override
    public PageList<T> queryByPage(BaseQuery query) {
        // queryByCount 总条数
        Integer totals = mapper.queryByCount(query);
        if (totals <= 0){
            return new PageList<>(0, new ArrayList<>());
        }
        // 当前分页展示的数据
        List<T> rows = mapper.queryByPage(query);
        return new PageList<>(totals, rows);
    }

}
